<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="initMethod"/>
      <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
        <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="initMethod">搜索</el-button>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="tableData.entity" stripe border size="small" style="width: 100%;">
      <el-table-column prop="id" label="设备编号" width="95px"/>
      <el-table-column prop="shopId" label="商戶ID" width="80px"/>
      <el-table-column prop="sn" label="设备SN" width="125px"/>
      <el-table-column prop="shopName" label="商铺名称" show-overflow-tooltip/>
      <el-table-column prop="agentName" label="代理名称" width="95px"/>
      <el-table-column prop="orderCount" label="订单数" align="center" width="95px"/>
      <el-table-column prop="orderAmount" label="订单收益(元)" align="center" width="100px"/>
      <el-table-column prop="version" label="款式" width="60px;">
        <template slot-scope="scope">
          <span v-if="scope.row.version===2" style="color: darkred">密码款</span>
          <span v-else-if="scope.row.version===3" style="color: darkgreen">蓝牙款</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="房间号" align="center" width="95px"/>
      <el-table-column prop="description" label="设备描述" show-overflow-tooltip/>
      <el-table-column prop="created" label="创建时间" width="150px" show-overflow-tooltip/>
      <el-table-column label="操作" fixed="right" width="200px" align="center">
        <template slot-scope="scope">
          <el-tooltip slot="reference" content="订单详情" placement="top" >
            <el-button type="primary" size="mini" icon="el-icon-document-copy" @click="orderDetail(scope.row.sn)"/>
          </el-tooltip>
          <el-tooltip slot="reference" content="商铺详情" placement="top">
            <el-button type="warning" size="mini" icon="el-icon-office-building" @click="shopDetail(scope.row.shopId)"/>
          </el-tooltip>
          <el-popover :ref="scope.row.id" placement="top" width="180">
            <p>确定回收设备吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subRefund(scope.row)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini">回收</el-button>
          </el-popover>

        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <div class="block">
      <el-pagination
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.size"
        :total="tableData.numberOfElements"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { postRequest, deleteRequest } from '@/api/device'
export default {
  data() {
    return {
      loading: false,
      delLoading: false,
      query: {
        value: '',
        type: '',
        page: 1,
        size: 10
      },
      tableData: {
        numberOfElements: 0,
        entity: []
      },
      queryTypeOptions: [
        { key: 'sn', display_name: '设备SN' },
        { key: 'shopName', display_name: '商户名称' },
        { key: 'shopId', display_name: '商店ID' },
        { key: 'agentId', display_name: '代理ID' },
        { key: 'agent', display_name: '代理名称' }
      ]
    }
  },
  created() {
    if (Object.keys(this.$route.query).length !== 0) {
      this.query.value = this.$route.query.value
      this.query.type = this.$route.query.type
    }
    this.$nextTick(() => {
      this.initMethod()
    })
  },
  methods: {
    checkPermission,
    initMethod() {
      postRequest('/pc/device', this.query).then(resp => {
        console.log(resp)
        this.tableData = resp.data
      })
    },
    subRefund(data) {
      deleteRequest('/pc/device/', data.sn).then(resp => {
        console.log(resp)
        this.initMethod()
        this.$refs[data.id].doClose()
      })
    },
    handleSizeChange(val) {
      this.query.size = val
      this.initMethod()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.initMethod()
    },
    orderDetail(sn) {
      this.$router.push({ path: 'order', query: { value: sn, type: 'devicesn' }})
    },
    shopDetail(shopId) {
      this.$router.push({ path: 'shop', query: { value: shopId, type: 'shopId' }})
    }
  }
}
</script>

<style scoped>
  .el-button + .el-button {
    margin-left: 0;
  }
</style>
